var crypto  = require('crypto');
global.clone    = require('clone');
global.http     = require('http');
global.util     = require('util');
global.common   = require('./common.js');
global.config   = require(require('fs').existsSync('../config.js') ? '../config.js' : './config.js');

requestGame = function(query, resp, callback){
    var options = {
        host: config.ServerHost,
        port: config.GameListen,
        path: '/',
        method: 'POST'
    };

    var req = http.request(options, function(res) {
        var chunks = [];
        res.on('data', function(chunk){
            chunks.push(chunk);
        });

        res.on('end', function(){
            var data = Buffer.concat(chunks).toString();
            var UniverseResp = null;
            try{
                UniverseResp = JSON.parse(data); 
            }catch(error){
                UniverseResp = null;
            }

            if( !UniverseResp ) {
                resp.code = 1; resp.desc='request universe error';
            }else{
                resp.code = UniverseResp.code;
                resp.desc = UniverseResp.desc;
                resp.data = UniverseResp.data;
            }

            callback && callback();
        });
    });

    req.on('error', function(err){
        resp.code = 1;
        resp.desc = 'request cloud error';
        console.log(util.format('err=%j', err));
        callback && callback(null);
    });

    req.end(util.format('mod=%s&act=%s&uid=%s&auth_time=%s&auth_key=%s&args=%j', 
        query.mod, query.act, query.uid, query.auth_time, query.auth_key, query.args));
    req.end();
}

function genAuthKey(time, uid) {
    var authKey = 'Keep it simple stupid';
    var md5 = crypto.createHmac('sha1', authKey);
    var rawKey = uid + '-' + time;
    return md5.update(rawKey).digest('base64').substring(0, 10);
}





query = {mod: 'activity', act: 'get_standard_activity_info', args: {key:'luckycharge'}};
query = {mod: 'activity', act: 'luckycharge_refresh', args: {}};
query = {mod: 'activity', act: 'luckycharge_throw', args: {}};
query = {mod: 'activity', act: 'luckycharge_get_rank', args: {}};
query = {mod: 'activity', act: 'luckycharge_get_rank_award', args: {}};
query = {mod: 'activity', act: 'worshipsaint_get_daily_award', args: {}};
query = {mod: 'activity', act: 'worshipsaint_get_level_award', args: {}};
query = {mod: 'activity', act: 'worshipsaint_worship', args: {}};
query = {mod: 'activity', act: 'get_standard_activity_info', args: {key:'worshipsaint'}};
query = {mod: 'smashland', act: 'init', args: {}};
query = {mod: 'smashland', act: 'attack', args: {id:1, level:1, team:{'4':1, '5':302}}};

query = {mod: 'herolog', act: 'get', args: {}};
query = {mod: 'herolog', act: 'battle', args: {chapId:1, chkptId:1}};
query = {mod: 'herolog', act: 'play_chapter_animation', args: {chapId:1}};

var time = common.getTime();
query.uid = 110100002;
query.auth_time = time;
query.auth_key = genAuthKey(time, query.uid);

var resp = {code:0, desc:'', data:{}};

requestGame(query, resp, function() {
    console.log(util.format('query=%j', query));
    console.log(util.format('resp=%j', resp));
    process.exit();
});
